// 
// Copyright (c) 2006-2009 Ben Motmans
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// Author(s):
//    Ben Motmans <ben.motmans@gmail.com>
//

using System;
using System.Text;
using System.Threading;
using System.Collections.Generic;

namespace Anculus.Core
{
	/// <summary>
	/// 
	/// </summary>
	public abstract class AbstractLogBackend : ILogBackend
	{
		protected ILogFormatter formatter;
		
		/// <summary>
		/// Initializes a new instance of the <see cref="AbstractLogBackend"/> class.
		/// </summary>
		protected AbstractLogBackend (ILogFormatter formatter)
		{
			LogFormatter = formatter;
		}
		
		protected AbstractLogBackend ()
		{
			LogFormatter = new DefaultLogFormatter ();
		}
		
		/// <summary>
		/// Gets a value indicating whether the log backend is open.
		/// </summary>
		/// <value>
		/// 	<c>true</c> if the log backend is open; otherwise, <c>false</c>.
		/// </value>
		public abstract bool IsOpen { get; }
		
		public ILogFormatter LogFormatter
		{
			get { return formatter; }
			set {
				if (value == null)
					throw new ArgumentNullException ("LogFormatter");
				
				formatter = value;
			}
		}

		
		
		public virtual void WriteEntry (LogEntry entry)
		{
			if (entry == null)
				return;

			Write (formatter.Format (entry));
		}

		/// <summary>
		/// Writes the specified raw message to the log.
		/// </summary>
		/// <param name="raw">The raw message.</param>
		public abstract void Write (string raw);

		/// <summary>
		/// Opens the log backend.
		/// </summary>
		public virtual void Open ()
		{
			//nothing
		}

		/// <summary>
		/// Closes the log backend.
		/// </summary>
		public virtual void Close ()
		{
			//nothing
		}

		/// <summary>
		/// Loads the settings from a config file.
		/// </summary>
		/// <param name="section">The section defining the ILogBackend parameters.</param>
		public virtual void LoadSettings (IConfigurationSection section)
		{
			formatter.LoadSettings (section["LogFormat"]);
		}
		
		/// <summary>
		/// Determines whether the specified <see cref="System.Object"/> is equal to this instance.
		/// </summary>
		/// <param name="obj">The <see cref="System.Object"/> to compare with this instance.</param>
		/// <returns>
		/// 	<c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>.
		/// </returns>
		/// <exception cref="T:System.NullReferenceException">
		/// The <paramref name="obj"/> parameter is null.
		/// </exception>
		public override bool Equals (object obj)
		{
			if (obj == null)
				return false;
			
			return obj.GetHashCode () == GetHashCode ();
		}

		/// <summary>
		/// Returns a hash code for this instance.
		/// </summary>
		/// <returns>
		/// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. 
		/// </returns>
		public override int GetHashCode ()
		{
			throw new NotImplementedException ("This method should be overriden by each subclass.");
		}
	}
}
